package com.poet.df.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.beetl.core.GroupTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.poet.df.dbinfo.DbInfo;
import com.poet.df.dbinfo.pojo.Table;
import com.poet.df.entity.Form;
import com.poet.df.entity.FormLine;
import com.poet.df.service.DatabaseMetaDataService;
import com.poet.df.service.DynamicFormService;
import com.poet.df.xml.DynamicFormXmlReader;
import com.poet.df.xml.pojo.HtmlForm;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

@Controller
@RequestMapping("/")
public class DynamicFormController {

	@Resource
	private DynamicFormService dfService;
	
	@Resource
	private DatabaseMetaDataService dbmdService;
	
	@Resource
	private DbInfo dbInfo;
	
	@Resource
	private DynamicFormXmlReader reader;
	
	@RequestMapping("forms")
	public String forms(ModelMap map){
//		dbInfo.loadDataBaseInfos();
//		List<Form> forms = dfService.getAllForms();
//		List<Table> tables = dbmdService.getTables();
		List<Table> tables = dbInfo.getAllTables();
		map.put("tables", tables);
//		map.put("tables", tables);
		return "forms";
	}
	
	@RequestMapping(value="saveForm",method={RequestMethod.POST})
	public String saveForm(Form form,ModelMap map){
		boolean saveFlag = dfService.saveForm(form);
		return "redirect:/forms.do";
	}
	
	@RequestMapping("formLinesByFormId")
	public String formLinesByFormId(Integer formId,ModelMap map){
		List<FormLine> lines = dfService.getFormLinesById(formId);
		map.put("lines", lines);
		return "formLines";
	}
	
	@Resource
	private Configuration freemarkerConfiguration;
	
	@RequestMapping(value="showForm",produces={"application/json;charset=utf-8"})
//	@ResponseBody
	public String showForm(String tableName,HttpServletResponse response){
//		Table table = dbInfo.getTableInfo(tableName);
//		Map<String, Table> map = new HashMap<String, Table>();
//		map.put("table", table);
//		try {
//			Writer out = response.getWriter();
//			Template template = freemarkerConfiguration.getTemplate("form.ftl");
//			template.process(map, out);
//		} catch (IOException e) {
//			e.printStackTrace();
//		} catch (TemplateException e) {
//			e.printStackTrace();
//		}
		
		return "showForm";
	}
	
/*	@Resource
	private GroupTemplate groupTemplate;*/
	
	@RequestMapping(value="loadForm",produces={"application/json;charset=utf-8"})
	@ResponseBody
	public void loadForm(String tableName,HttpServletResponse response){
		if(tableName == null || tableName.isEmpty()){
			tableName = "column_display";
		}
		HtmlForm form = reader.getForm(tableName);
		Map<String, HtmlForm> map = new HashMap<String, HtmlForm>();
		map.put("htmlForm", form);
		try {
			Writer out = response.getWriter();
			Template template = freemarkerConfiguration.getTemplate("form.ftl");
			template.process(map, out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
